package org.smartj2ee.db;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.ConnectionFactory;
import org.apache.commons.dbcp.DriverConnectionFactory;
import org.apache.commons.dbcp.SQLNestedException;
import org.osgi.framework.Bundle;

public class SmartDataSource {
	private static MyDataSource dataSource;
	static List<Connection> conns=new LinkedList<Connection>();
    public static BasicDataSource getDataSource(){
    	
    	return dataSource;
    }
    public static void init(Class driverCls){
        if(dataSource==null){
        	dataSource=new MyDataSource(driverCls);
        	//dataSource.setDriverClassName(driverName);
        	int initSize=3;
        	dataSource.setInitialSize(initSize);
        	dataSource.setUrl("jdbc:mysql://127.0.0.1:3306/test");
    		dataSource.setUsername("root");
    		dataSource.setPassword("123");
    	}
    	
    }
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		
	}

}

class MyDataSource extends BasicDataSource{
	private Bundle bundle;
	private Class driverCls;
	public MyDataSource(Class driverCls){//Bundle bundle,String driverClassName
		//this.bundle=bundle;
		//this.driverClassName=driverClassName;
		this.driverCls=driverCls;
	}
	
	protected ConnectionFactory createConnectionFactory() throws SQLException {
        // Load the JDBC driver class
        Class driverFromCCL =driverCls;// null;
       /** if (driverClassName != null) {
            try {
                try {
                    if (driverClassLoader == null) {
                       // Class.forName(driverClassName);
                    	this.bundle.loadClass(driverClassName);
                    } else {
                        Class.forName(driverClassName, true, driverClassLoader);
                    }
                } catch (ClassNotFoundException cnfe) {
                    driverFromCCL = Thread.currentThread(
                            ).getContextClassLoader().loadClass(
                                    driverClassName);
                }
            } catch (Throwable t) {
                String message = "Cannot load JDBC driver class '" +
                    driverClassName + "'";
                logWriter.println(message);
                t.printStackTrace(logWriter);
                throw new SQLNestedException(message, t);
            }
        }*/

        // Create a JDBC driver instance
        Driver driver = null;
        try {
            if (driverFromCCL == null) {
                driver = DriverManager.getDriver(url);
            } else {
                // Usage of DriverManager is not possible, as it does not
                // respect the ContextClassLoader
                driver = (Driver) driverFromCCL.newInstance();
                if (!driver.acceptsURL(url)) {
                    throw new SQLException("No suitable driver", "08001"); 
                }
            }
        } catch (Throwable t) {
            String message = "Cannot create JDBC driver of class '" +
                (driverClassName != null ? driverClassName : "") +
                "' for connect URL '" + url + "'";
            logWriter.println(message);
            t.printStackTrace(logWriter);
            throw new SQLNestedException(message, t);
        }

        // Can't test without a validationQuery
        if (validationQuery == null) {
            setTestOnBorrow(false);
            setTestOnReturn(false);
            setTestWhileIdle(false);
        }

        // Set up the driver connection factory we will use
        String user = username;
        if (user != null) {
            connectionProperties.put("user", user);
        } else {
            log("DBCP DataSource configured without a 'username'");
        }

        String pwd = password;
        if (pwd != null) {
            connectionProperties.put("password", pwd);
        } else {
            log("DBCP DataSource configured without a 'password'");
        }

        ConnectionFactory driverConnectionFactory = new DriverConnectionFactory(driver, url, connectionProperties);
        return driverConnectionFactory;
    }
}